var util =
		{
			applyDefaults: function(object, defaults)
				{
					if(object._maxLength && object._maxLength != Number.MAX_VALUE)
					{
						var backup = {maxLength: object._maxLength};
					}
					Ext.apply(object, defaults);
					if(backup)
					{
						Ext.apply(object, backup);
					}
				},
			removeField : function(field)
				{
					if(field && field.el)
					{
						field.el.up('.x-form-item').remove();
						this.remove(field);
						this.doLayout();
					}
				},
			createComplementoField : function(params)
				{
					var object = 
						{
							dir : new Ext.form.Hidden
							    ({
							        name: 'data[Complementos]'+params.basename.field+'[dir]'
							    }),
							foreign_key :
								{
									name: new Ext.form.Hidden
									    ({
									        name: 'data[Complementos]'+params.basename.field+'[foreign_key][name]'
									    }),
									value: new Ext.form.Hidden
									    ({
									        name: 'data[Complementos]'+params.basename.field+'[foreign_key][value]'
									    })
								},
							id : new Ext.form.Hidden
							    ({
							        name: 'data[Complementos]'+params.basename.field+'[id]'
							    }),
							tipo_id : new Ext.form.Hidden
							    ({
							        name: 'data[Complementos]'+params.basename.field+'[tipo_id]'
							    }),
							text : new Ext.form.TextArea
							    ({
							        fieldLabel: params.title.text,
							        name: 'data[Complementos]'+params.basename.field+'[texto]',
							        width: params.width,
							        allowBlank: params.allowBlank.text,
							        height: 80,
									_maxLength: 9999999
							    }),
							clean: function(force)
								{
									if(force)
									{
										this.dir.setValue('');
										this.foreign_key.name.setValue('');
										this.foreign_key.value.setValue('');
										this.tipo_id.setValue('');
									}
									this.id.setValue('');
									this.text.setValue('');
									this.text.clearInvalid();
								},
							setDisabled: function(option)
								{
									this.dir.setDisabled(option);
									this.foreign_key.name.setDisabled(option);
									this.foreign_key.value.setDisabled(option);
									this.tipo_id.setDisabled(option);
									this.id.setDisabled(option);
									this.text.setDisabled(option);
								}
						};
						
					if(params.onchange && typeof params.onchange.text == 'function')
					{
						object.text.on('change', params.onchange.text);
					}
					
					if(params.title.file && params.basename.file)
					{
						object.file = new Ext.form.MiscField
							    ({
							        fieldLabel: params.title.file,
							        width: params.width,
							        value: '<input type="file" id="'+params.id+'" name="data[Files]['+params.basename.file+'_file]" onchange="row.'+params.id+'.file.onchange()" /><span id="'+params.id+'_anexo_ctrl" style="display: none;">&nbsp;<img src="'+_PHP_.url.imgroot+'cross.png" alt="" style="cursor: pointer;" onclick="anexo.del(\''+params.id+'\', row.'+params.id+'.file.url.del)">&nbsp;<img src="'+_PHP_.url.imgroot+'disk.png" alt="" style="cursor: pointer;" onclick="anexo.get(\''+params.id+'\', row.'+params.id+'.file.url.get)"></span><iframe id="'+params.id+'_anexo_dl" style="display: none;"></iframe>',
							        toggleCtrl: function(option)
										{
											if(option !== null)
											{
												Ext.get(params.id+'_anexo_ctrl')[(option === true ? 'show' : 'hide')]();
											}
											else
											{
												Ext.get(params.id+'_anexo_ctrl').toggle();
											}
										},
							        url: 
							        	{
							        		get: null,
							        		del: null
							        	},
									onchange: (params.onchange && typeof params.onchange.file == 'function') ? params.onchange.file : Ext.emptyFn,
									setDisabled: function(option)
										{
											Ext.get(params.id).dom.disabled = option;
										},
									setValue: function(value)
										{
											Ext.get(params.id).dom.value = value;
										},
									getValue: function()
										{
											return Ext.get(params.id).getValue();
										}
							    });
						object._clean = object.clean;
						object.clean = function(force)
							{
								this._clean(force);
								this.file.setValue('');
							};
						object._setDisabled = object.setDisabled;
						object.setDisabled = function(option)
							{
								this._setDisabled(option);
								this.file.setDisabled(option);
							};
					}
					for(var field in params.values)
					{
						object[field].setValue(params.values[field]);
					}
					
					return object;
				},
			createFrame: function(params)
		    	{
					params.callback.list = (typeof params.callback.list == 'function') ? params.callback.list : null;
					params.callback.add = (typeof params.callback.add == 'function') ? params.callback.add : null;
					
					Ext.Updater.defaults.loadScripts = true;
					var panel = new Ext.Panel
						({
							autoLoad: params.url.list,
							loadParams: 
								{
									url: params.url.list,
									method: 'POST',
									discardUrl: false,
								    nocache: true,
								    scripts: true
								}
						});
					var fieldset = new Ext.form.FieldSet
						({
					        title: params.title,
			                layout: 'form',
							bodyStyle: 'padding-top: 5px',
							autoScroll: true,
			                height: params.height,
			                collapsible: true,
			                collapsed: params.collapsed,
					       	tbar: 
						    	[
									new Ext.Button
										({
									        text: 'Listar',
									        icon: _PHP_.url.imgroot+'application_cascade.png',
									        cls: 'x-btn-text-icon',
									        handler: params.callback.list
									    }),
									new Ext.Button
										({
									        text: 'Adicionar',
									        icon: _PHP_.url.imgroot+'application_add.png',
									        cls: 'x-btn-text-icon',
											handler: function()
												{
													fieldset.setTitle(params.title);
													params.callback.add();
												}
									    })
								],
							items: panel,
							getPanel: function()
								{
									return panel;
								},
							callback:
								{
									list: params.callback.list,
									add: params.callback.add
								},
							editRow: function(params)
								{
									params.text = Ext.query(params.elId+' .texto')[0].innerHTML;
									params.file = Ext.query(params.elId+' img[src*=disk]').length > 0 ? true : false;
									fieldset.callback.add();
									
									if(typeof params.callback == 'function')
							        {
							        	params.callback(params);
							        }
								},
							delRow: function(params)
								{
									var msg = Ext.MessageBox.wait('Processando ...', 'Aguarde');
									
									Ext.Ajax.request
										({
										   url: params.url,
										   method: 'POST',
										   callback: function(options, success, response)
										       {
										       	   msg.hide();
												   delete msg;
												   //alert(response.responseText); return false;
												   
												   if(!success)
										       	   {
										                Ext.MessageBox.alert('Erro', 'Não foi possivel conectar-se com o servidor, por favor tente novamente');
										           }
										           else if(typeof params.callback == 'function')
										           {
										               response = Ext.util.JSON.decode(response.responseText); 
													   if(response.success == 'true')
													   {
													       params.callback();
													   }
										           }
										   	   }
										});
								}
					    });
					 
					 return fieldset;
				}
		};